package com.bringway.p2p.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.sf.json.JSONObject;

import com.bringway.framework.domain.SysBizAtt;
import com.bringway.global.dao.SysAttechmentDao;
import com.bringway.global.util.StringUtils;
import com.bringway.p2p.dao.JhnxmDao;
import com.bringway.p2p.service.JhnxmService;



/**
 * 
 * @author js
 * @version 创建时间：2016年7月4日 
 * @功能：计划内项目管理界面
 */

@Service
@Transactional
public class JhnxmServieImpl implements JhnxmService{
	
	Logger logger = Logger.getLogger(JhnxmServieImpl.class);
	
	@Autowired
	private JhnxmDao jhnxmDao;
	@Autowired
	private SysAttechmentDao sysAttechmentDao;

	/**
	 * @作者 js  
	 * @方法名 queryJhnzqList  
	 * @功能 TODO 
	 * @param params
	 * @return
	 */
	@Override
	public List queryJhnzqList(JSONObject params) {
		return jhnxmDao.queryJhnzqList(params);
	}

	/**
	 * @作者 js  
	 * @方法名 getJhnzqCount  
	 * @功能 TODO 
	 * @param params
	 * @return
	 */
	@Override
	public int getJhnzqCount(JSONObject params) {
		return jhnxmDao.getJhnzqCount(params);
	}

	/**
	 * @作者 js  
	 * @方法名 saveJhnzq  
	 * @功能 TODO 
	 * @param params
	 * @return
	 */
	@Override
	public boolean saveJhnzq(JSONObject params) {
		String zqid=jhnxmDao.saveJhnzq(params);
		String atts = params.optString("atts");
		String[] att = atts.split(",");
		for (int i = 0; i < att.length; i++) {
			if("".equals(att[i]))continue;
			SysBizAtt bizAtt = new SysBizAtt();
			bizAtt.setBizId(zqid);
			bizAtt.setAttId(Long.decode(att[i]));
			sysAttechmentDao.createBizAtt(bizAtt);
		}
		if (StringUtils.isNotEmpty(zqid)) {
			return true;
		}
		return false;
	}

	@Override
	public void deleteJhnzq(JSONObject params) {
		jhnxmDao.deleteJhnzq(params);
	}

	/**
	 * @作者 js  
	 * @方法名 getJhnzqInfo  
	 * @功能 TODO 
	 * @param params
	 * @return
	 */
	@Override
	public Map<String, Object> getJhnzqInfo(JSONObject params){
		return jhnxmDao.getJhnzqInfo(params);
	}

	/**
	 * @作者 js  
	 * @方法名 queryJhncgList  
	 * @功能 TODO 
	 * @param params
	 * @return
	 */
	@Override
	public List queryJhncgList(JSONObject params) {
		return jhnxmDao.queryJhncgList(params);
	}

	/**
	 * @作者 js  
	 * @方法名 getJhncgCount  
	 * @功能 TODO 
	 * @param params
	 * @return
	 */
	@Override
	public int getJhncgCount(JSONObject params) {
		return jhnxmDao.getJhncgCount(params);
	}
	/**
	 * @作者 js  
	 * @方法名 saveJhncg  
	 * @功能 TODO 
	 * @param params
	 * @return
	 */
	@Override
	@Transactional("p2p")
	public boolean saveJhncg(JSONObject params) {
		boolean isSaveJhncgSuc=false;
		boolean isUpdateJhnzqSuc=false;
		boolean isSaveJhnxmzqSuc=false;
		//1.保存到项目草稿
		String xmcgid=jhnxmDao.saveJhncg(params);
		isSaveJhncgSuc=StringUtils.isNotEmpty(xmcgid);
		if (isSaveJhncgSuc) {
			logger.info("saveJhncg 保存草稿成功!");
			String gridId=params.optString("gridId");
			String[] gridIds=gridId.split(",");
			String gridFbje=params.optString("gridFbje");
			String[] gridFbjes=gridFbje.split(",");
			//2.更新债权表金额
			int i=0;
			for (String string : gridIds) {
				params.put("zq_id", string);
				params.put("fbje", gridFbjes[i]);
				params.put("xmzt", "1");
				isUpdateJhnzqSuc=jhnxmDao.updateJhnzqById(params);
				i++;
				if (isUpdateJhnzqSuc) {
					logger.info("saveJhncg 更新债权表1条记录金额成功!"+string);
				}else {
					logger.info("saveJhncg 更新债权表1条记录金额失败!"+string);
					return false;
				}
			}
			logger.info("saveJhncg 更新债权表选择的记录金额全部成功!");
			//3.保存关联关系到xmzq
			int j=0;
			for (String string : gridIds) {
				params.put("xmcgid", xmcgid);
				params.put("zqid", string);
				params.put("fbje", gridFbjes[j]);
				isSaveJhnxmzqSuc=jhnxmDao.saveJhnxmzq(params);
				j++;
				if (isSaveJhnxmzqSuc) {
					logger.info("saveJhncg 新增项目债权表1条记录状态成功!");
				}else {
					logger.info("saveJhncg 新增项目债权表1条记录状态失败!");
					//throw new RuntimeException();
					return false;
				}
			}
			logger.info("saveJhncg 更新债权表选择的记录状态全部成功!");
			
			
		} else {
			logger.info("saveJhncg 保存草稿失败!");
			return false;
		}
		return true;
	}

	@Override
	@Transactional("p2p")
	public boolean deleteJhncg(JSONObject params) {
		boolean isDeleteJhncgSuc=false;
		boolean isUpdateJhnzqSuc=false;
		boolean isDeleteJhnxmzqSuc=false;
		
		//1.删除项目草稿
		isDeleteJhncgSuc=jhnxmDao.deleteJhncg(params)>0?true:false;
		if (isDeleteJhncgSuc) {
			logger.info("saveJhncg 删除项目草稿成功!");
			//2.查询项目债权表中本项目的所有债权id
			List<Map<String, Object>> list=jhnxmDao.queryJhnxmzqList(params);
			//3.删除项目债权表中本项目所有记录
			isDeleteJhnxmzqSuc=jhnxmDao.deleteJhnxmzq(params)>0?true:false;
			if (isDeleteJhnxmzqSuc) {
				logger.info("deleteJhncg xmzq删除记录成功!");
			}else {
				logger.info("deleteJhncg xmzq删除记录失败!");
				return false;
			}
			//4.更新债权表中状态
			int i=0;
			for (Map<String, Object> map : list) {
				params.put("zq_id", map.get("ZQID"));
				params.put("fbje",MapUtils.getString(map, "FBJE"));
				isUpdateJhnzqSuc=jhnxmDao.updateJhnzqById(params);
				i++;
				if (isUpdateJhnzqSuc) {
					logger.info("deleteJhncg 更新债权表1条记录状态成功!");
				}
			}
			logger.info("deleteJhncg 更新债权表所有记录状态成功!");
			
		}
		return true;
	}

	@Override
	public Map<String, Object> getJhncgInfo(JSONObject params) {
		return jhnxmDao.getJhncgInfo(params);
	}

	/**
	 * @作者 js  
	 * @方法名 getQxDayByQxMonth  
	 * @功能 TODO 
	 * @param qxMonth
	 * @return
	 */
	@Override
	public int getQxDayByQxMonth(String qxMonth) {
		return jhnxmDao.getQxDayByQxMonth(qxMonth);
	}

	@Override
	public List queryJhncgViewList(String xmcgid) {
		JSONObject params=new JSONObject();
		List<Map<String, Object>> list=new ArrayList<>();
		params.put("bizId", xmcgid);
		List<Map<String, Object>> xmzqs=jhnxmDao.queryJhnxmzqList(params);
		//循环查询
		for (Map<String, Object> map : xmzqs) {
			String zqid=MapUtils.getString(map,"ZQID");
			String fbje=MapUtils.getString(map,"ZQ_FBJE");
			Map<String, Object> map2=jhnxmDao.queryJhnzqById(zqid);
			map2.put("FBJE", fbje);
			list.add(map2);
		}
		return list;
	}

	@Override
	public int getJhncgViewCount(String xmcgid) {
		return jhnxmDao.getJhncgViewCount(xmcgid);
	}

	@Override
	@Transactional("p2p")
	public boolean saveJhnsh(JSONObject params) {
		/*
		 * 1.更新cg中审核信息和状态
		 * 如不通过,
		 * 2.复制cg中记录到xm中
		 * 3.更新xmzq中xm字段
		 */
		boolean isUpdatecgSuc=false;
		boolean isUpdatexmzqSuc=false;
		boolean isUpdateJhnzqSuc=false;
		params.put("bizId", params.opt("cgid"));
		isUpdatecgSuc=jhnxmDao.updateJhncg(params)>0?true:false;//更新草稿数据
		if (isUpdatecgSuc) {
			logger.info("saveJhnsh cg更新记录成功!");
			String xmzt=params.optString("xmzt");
			if ("21".equals(xmzt)) {//审核通过
				List<Map<String, String>> list=jhnxmDao.queryJhnxmzqList(params);
				for (Map<String, String> map : list) {
					params.put("zq_id", MapUtils.getString(map, "ZQID"));
					params.put("fbje", MapUtils.getString(map, "ZQ_FBJE"));
					isUpdateJhnzqSuc=jhnxmDao.updateJhnzqById(params);
				}
				if (isUpdateJhnzqSuc) {
					logger.info("saveJhnsh 更新债权表所有记录金额,成功!审核通过状态");
				}
				
			}else {
				List<Map<String, String>> list=jhnxmDao.queryJhnxmzqList(params);
				for (Map<String, String> map : list) {
					params.put("zq_id", MapUtils.getString(map, "ZQID"));
					params.put("fbje", MapUtils.getString(map, "ZQ_FBJE"));
					isUpdateJhnzqSuc=jhnxmDao.updateJhnzqById(params);
				}
				if (isUpdateJhnzqSuc) {
					logger.info("saveJhnsh 更新债权表所有记录金额,成功!审核不通过状态");
				}
				return false;
			} 
			Map<String, Object> paramMap =jhnxmDao.getJhncgInfo(params);//获得cg对应记录
			String xmid=jhnxmDao.saveJhnxm(paramMap);//复制cg记录到xm中
			if (null!=xmid) {
				logger.info("saveJhnsh 复制cg记录到xm中成功!");
				params.put("set_xmid", xmid);
				isUpdatexmzqSuc=jhnxmDao.updateJhnxmzq(params)>0?true:false;//更新xmzq中记录
				if (isUpdatexmzqSuc) {
					logger.info("saveJhnsh 更新xmzq中记录成功!");
				}
			}
		} 
		logger.info("saveJhnsh 审核成功!");
		return true;
	}

	@Override
	public List queryJhnlbList(JSONObject params) {
		return jhnxmDao.queryJhnlbList(params);
	}

	@Override
	public int getJhnlbCount(JSONObject params) {
		return jhnxmDao.getJhnlbCount(params);
	}

	@Override
	public List<Map<String, Object>> queryJhnxmzqViewList(JSONObject params) {
		//查询置换前记录
		List<Map<String, Object>> list=new ArrayList<>();
		List<Map<String, Object>> xmzqs=jhnxmDao.queryJhnxmzqList(params);
		//查询置换申请中的记录
		String zhzt=params.optString("zhzt");
		String zqidOut=null;
		String zqidIn=null;
		Object sqr=null;
		Object sqsj=null;
		Object sqbz=null;
		if ("01".equals(zhzt)) {
			Map<String, Object> zhsqMap =jhnxmDao.queryJhnzhsqList(params);
			if (null!=zhsqMap) {
				zqidOut=MapUtils.getString(zhsqMap,"REF_JHN_ZHC_ZQ_ID");
				zqidIn=MapUtils.getString(zhsqMap,"REF_JHN_ZHR_ZQ_ID");
				sqr=zhsqMap.get("TQSH_SQ_USER_ID");
				sqsj=zhsqMap.get("TQSH_SQ_ZH_DATE");
				sqbz=zhsqMap.get("TQSH_SQ_CONTENT");
			}
		}
		//循环查询
		String cgid=MapUtils.getString(xmzqs.get(0),"CGID");
		for (Map<String, Object> map : xmzqs) {
			String zqid=MapUtils.getString(map,"ZQID");
			Map<String, Object> zqMap=jhnxmDao.queryJhnzqById(zqid);
			if (zqid.equals(zqidOut)) {
				zqMap.put("IS_TQSH_ZH", "01");
			}
			zqMap.put("CG_ID", cgid);
			zqMap.put("TQSH_SQR", sqr);
			zqMap.put("TQSH_SQSJ", sqsj);
			zqMap.put("TQSH_SQBZ", sqbz);
			list.add(zqMap);
		}
		if (null!=zqidIn) {
			Map<String, Object> zqMap=jhnxmDao.queryJhnzqById(zqidIn);
				zqMap.put("IS_TQSH_ZH", "02");
				zqMap.put("CG_ID", cgid);
				zqMap.put("TQSH_SQR", sqr);
				zqMap.put("TQSH_SQSJ", sqsj);
				zqMap.put("TQSH_SQBZ", sqbz);
				list.add(zqMap);
		}
		return list;
	}

	@Override
	public int getJhnxmzqViewCount(JSONObject params) {
		return jhnxmDao.getJhnxmzqViewCount(params);
	}

	/* 
	 * @方法名  saveJhnzhSQ 
	 * @功能   TODO 置换债权申请
	 * @param params
	 * @return  
	 * @see com.bringway.p2p.service.JhnxmService#saveJhnzhSQ(net.sf.json.JSONObject)  
	 */
	@Override
	@Transactional("p2p")
	public boolean saveJhnzhSQ(JSONObject params) {
		
		JSONObject paramsTemp=new JSONObject();
		paramsTemp.put("zq_zt", "01");
		paramsTemp.put("zq_id", params.optString("tqshOutId"));
		paramsTemp.put("zq_ref_zqid_in", params.optString("tqshInId"));
		boolean isUpdateZhztC=jhnxmDao.updateJhnzqById(paramsTemp);//更新置换出债权状态为置换中
		paramsTemp.put("zq_id", params.optString("tqshInId"));
		paramsTemp.put("zq_ref_zqid_in",null);
		boolean isUpdateZhztR=jhnxmDao.updateJhnzqById(paramsTemp);//更新置换入债权状态为置换中
		
		boolean isInsertJhnzhzqSq=jhnxmDao.insertJhnzhzqSq(params)>0?true:false;//新增申请情况
		
		if (isUpdateZhztC&&isUpdateZhztR&&isInsertJhnzhzqSq) {
			logger.info("saveJhnzhSQ 计划内债权置换申请,成功!");
			return true;
		}
		logger.info("saveJhnzhSQ 计划内债权置换申请,失败!");
		return false;
	}

	/* 
	 * @方法名  saveJhnzhSH 
	 * @功能   TODO 置换债权审核
	 * @param params
	 * @return  
	 * @see com.bringway.p2p.service.JhnxmService#saveJhnzhSH(net.sf.json.JSONObject)  
	 */
	@Override
	@Transactional("p2p")
	public boolean saveJhnzhSH(JSONObject params) {
		//1.更新zhzq_sq
		boolean isUpateJhnzhsqSh=jhnxmDao.updateJhnzhSq(params)>0?true:false;
		if (isUpateJhnzhsqSh) {
			logger.info("saveJhnzhSH 更新债权置换申请表状态和审核信息,成功!");
			String shzt=params.optString("shzt");
			if ("02".equals(shzt)) {
				JSONObject zqParams=new JSONObject();
				zqParams.put("zq_id", params.optString("tqshOutId"));
				zqParams.put("zq_zt", "02");
				zqParams.put("zq_ref_zqid_in", params.optString("tqshInId"));
				boolean isUpdateZhC=jhnxmDao.updateJhnzqById(zqParams);
				zqParams.put("zq_id", params.optString("tqshInId"));
				zqParams.put("zq_zt", "00");
				zqParams.put("djje", params.optString("djje"));
				zqParams.put("yyje", params.optString("yyje"));
				boolean isUpdateZhR=jhnxmDao.updateJhnzqById(zqParams);
				if (isUpdateZhC&&isUpdateZhR) {
					logger.info("saveJhnzhSH 审核通过,更新置债权状态为失效,成功!");
					jhnxmDao.updateJhnxmzq(params);
					zqParams.put("zqid", params.optString("tqshOutId"));
					List<Map<String, Object>> list= jhnxmDao.queryJhnxmzqList(zqParams);
					JSONObject xmzqParams=new JSONObject();
					for (Map<String, Object> map : list) {
						xmzqParams.put("xmcgid", MapUtils.getString(map, "CGID"));
						xmzqParams.put("zqid", params.optString("tqshInId"));
						xmzqParams.put("xmid", MapUtils.getString(map, "XMID"));
						xmzqParams.put("fbje", MapUtils.getString(map, "ZQ_FBJE"));
						xmzqParams.put("is_tqsh", "02");
						jhnxmDao.saveJhnxmzq(xmzqParams);
					}
					logger.info("saveJhnzhSH 更新债权匹配表,成功!");
					
				} else {
					logger.info("saveJhnzhSH 审核通过,更新置换出债权状态为失效,失败!");
					return false;
				}
				
			} else if ("03".equals(shzt)) {
				JSONObject zqParams=new JSONObject();
				zqParams.put("zq_id", params.optString("tqshOutId"));
				zqParams.put("zq_zt", "00");
				boolean isUpdateZhC=jhnxmDao.updateJhnzqById(zqParams);
				zqParams.put("zq_id", params.optString("tqshInId"));
				boolean isUpdateZhR=jhnxmDao.updateJhnzqById(zqParams);
				if (isUpdateZhC&&isUpdateZhR) {
					logger.info("saveJhnzhSH 审核不通过,还原置换的债权状态为可用,成功!");
				} else {
					logger.info("saveJhnzhSH 审核不通过,还原置换的债权状态为可用,失败!");
					return false;
				}
			}else {
				logger.info("saveJhnzhSH 审核状态有误!");
				return false;
			}
		} else {
			logger.info("saveJhnzhSH 更新债权置换申请表状态和审核信息,失败!");
			return false;
		}
		logger.info("saveJhnzhSH 置换债权审核成功!");
		return true;
	}

	@Override
	public List<Map<String, Object>> queryJhnzhList(JSONObject params) {
		return jhnxmDao.queryJhnzhList(params);
	}

	@Override
	public int getJhnzhCount(JSONObject params) {
		return jhnxmDao.getJhnzhCount(params);
	}

	@Override
	public List<Map<String, Object>> queryJhnzhViewList(JSONObject params) {
		List<Map<String, Object>> list=new ArrayList<>();
		String zqid=params.optString("zhc_zqid");
		Map<String, Object> map=jhnxmDao.queryJhnzqById(zqid);
		map.put("IS_ZHCR", "置换出");
		list.add(map);
		zqid=params.optString("zhr_zqid");
		if (StringUtils.isNotEmpty(zqid)) {
			map=jhnxmDao.queryJhnzqById(zqid);
			map.put("IS_ZHCR", "置换入");
			map.putAll(jhnxmDao.queryJhnzhsqList(params));
			list.add(map);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> queryTjrsjhmList(JSONObject params) {
		return jhnxmDao.queryTjrsjhmList(params);
	}

}
